package com.clkj.aimin.dao.user;

import com.clkj.aimin.pojo.user.UserOrder;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

@Repository
public interface UserOrderMapper extends Mapper<UserOrder> {
    @Select("<script>" +
            "select o.*,u.user_name ,d.doctor_name from t_cl_userOrder o " +
            "left join t_cl_user u on o.user_id=u.user_id " +
            "left join t_cl_doctor d on o.doctor_id = d.inuserId " +
            "where o.helper_id = #{userId} " +
            "<if test = 'flag==0'> and  o.pay_type = 0 </if>" +
            "<if test = 'flag==1'> and  o.pay_type = 0 and o.order_status  = 3 </if>" +
            "</script>")
    List<UserOrder> getOrderList(@Param("userId") Integer userId,Integer flag);

    @Select("select a.* ,b.user_name,c.user_name as doctorName from t_cl_userOrder a " +
            "left join t_cl_user b on a.user_id = b.user_id  " +
            "left join t_cl_user c on a.doctor_id = c.user_id " +
            "where a.parent_id = #{userId}")
    List<UserOrder> getUserOrderDetail(@Param("userId")Integer userId);

    @Select("select a.*,b.user_name from t_cl_userOrder a " +
            "left join t_cl_user b on a.helper_id = b.user_id  " +
            "where a.helper_id = #{userId} and a.order_status = 0 and a.order_type in (0,1)")
    List<UserOrder> listApply(Integer userId);

    @Select("select a.*,b.user_name,c.doctor_name from t_cl_userOrder a " +
            "left join t_cl_user b on a.user_id = b.user_id " +
            "left join t_cl_doctor c on a.doctor_id = c.inuserId  " +
            "where a.apply_id = #{userId} and a.order_status = 0 and a.order_type in (0,1)")
    List<UserOrder> listMyApply(@Param("userId")Integer userId);

    @Select("select a.*,b.user_name from t_cl_userOrder a " +
            "left join t_cl_user b on a.user_id = b.user_id " +
            "where a.doctor_id = #{doctorId} and dateDiff(day,visit_time,getDate()) = 0 and a.order_status = 1 ")
    List<UserOrder> doctorListApply(@Param("doctorId") Integer doctorId);

    @Select("select a.*,b.user_name as helpName,c.doctor_name from t_cl_userOrder a " +
            "left join t_cl_user b on a.helper_id = b.user_id " +
            "left join t_cl_doctor c on a.doctor_id = c.inuserId  " +
            "where a.user_id = #{userId}  order by a.order_id desc ")
    List<UserOrder> listMyApplyList(@Param("userId")Integer userId);
}
